#include "sphereWindow.h"
#include <QString>
#include <qmetaobject.h>
#include <qobject.h>
#include <qpushbutton.h>

#include "ui_sphereWindow.h"
#include "BRep_Tool.hxx"

sphereWindow::sphereWindow(modelSoft* myModelSoft, SphereFeature* m_sphereFeature, QWidget *parent)
	: myModelSoft(myModelSoft)
	,sphereFeature(m_sphereFeature)
	,QWidget(parent)
	,ui(new Ui::sphereWindowClass())
{
	ui->setupUi(this);
	ui->radius1Input->setText(QString::number(sphereFeature->getRadius1()));
	shapeID = sphereFeature->getShapeId();
	//查找该Pipeline是否已存在，存在则获取，不存在则创建
	auto iter1 = myModelSoft->m_pipelineMap.find(sphereFeature->getShapeId());
	if (iter1 == myModelSoft->m_pipelineMap.end()) {
	spherePipeline = myModelSoft->makeSphere(sphereFeature);
	spherePipeline->Actor()->SetPickable(false);
	}
	else {
		spherePipeline = iter1->second;
		edit = true;
		//已存在pipeline,高亮显示
		spherePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		myModelSoft->myView->getRenderWindow()->Render();
	}

	createConnections();
}



double sphereWindow::get_sphere_radius()
{
	QString radiusInput = ui->radius1Input->text();
	return radiusInput.toDouble();
}

void sphereWindow::closeEvent(QCloseEvent* event)
{
	if (!confirmCreate) {
		if (!spherePipeline.IsNull() && !edit) {
			myModelSoft->RemovePipelineFromRenderer(spherePipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
	}
	if (!spherePipeline.IsNull() && edit) {
		spherePipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);
		myModelSoft->myView->getRenderWindow()->Render();
	}
}

void sphereWindow::receivePos(const TopoDS_Shape& shape)
{
	const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
	// 获取顶点坐标
	if (!vertex.IsNull()) {
		point_pos = BRep_Tool::Pnt(vertex);
		sphereFeature->setLocation(point_pos);
		spherePipeline = myModelSoft->updateSpherePipelineShape(spherePipeline, sphereFeature);
		spherePipeline->Actor()->SetPickable(false);
	}
	else {
		qDebug() << "null vertex";
	}

}

void sphereWindow::onClickVertexSelect()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Vertex);
	myModelSoft->changeSelector(SM_Vertex);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void sphereWindow::onRadius1Changed()
{
	sphereFeature->setRadius1(ui->radius1Input->text().toDouble());
	spherePipeline = myModelSoft->updateSpherePipelineShape(spherePipeline, sphereFeature);
	spherePipeline->Actor()->SetPickable(false);

}

void sphereWindow::createConnections()
{
	//connect(ui->cancelButton, SIGNAL(&QPushButton::clicked), this, SLOT(close()));
	//上面这种连接方式为何不行???
	connect(ui->confirmButton, &QPushButton::clicked, this, &sphereWindow::makeSphere_onClick);
	connect(ui->cancelButton, &QPushButton::clicked, this, &QWidget::close);
	connect(ui->vertexSelectButton, &QPushButton::clicked, this, &sphereWindow::onClickVertexSelect);
	connect(ui->radius1Input, &QLineEdit::textChanged, this, &sphereWindow::onRadius1Changed);
	connect(myModelSoft->currVertexSelector, &OccVertexSelector::sentVertex, this, &sphereWindow::receivePos);

}

void sphereWindow::makeSphere_onClick() {
	confirmCreate = true;
	spherePipeline->Actor()->SetPickable(true);
	if(!edit){
	QStandardItem* item1 = new QStandardItem(QString("球体-%1").arg(spherePipeline->GetShapeID()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	}

	close();
}

sphereWindow::~sphereWindow()
{}
